package outsideInterface.aj.controller;

import aj.bo.AjActivationCode;
import aj.bo.AjAd;
import aj.bo.AjJs;
import aj.bo.AjPhone;
import aj.service.AjActivationCodeService;
import aj.service.AjAdService;
import aj.service.AjJsService;
import aj.service.AjPhoneService;
import framework.common.Common;
import framework.common.ResponseVo;
import framework.util.fileUpload.service.SysFileService;
import framework.util.jdkExt._TimeUtil;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.OrderBy;
import framework.util.queryFilter.vo.Where;
import frameworkExt.util.controller.BaseControllerExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 脚本管理的外部接口
 * @author 吴宇亮 on 2019/10/31 0031 下午 2:11
 */
@RestController
@RequestMapping("/outsideInterface/AjOutsideInterface.do")
public class AjOutsideInterface extends BaseControllerExt {

    @Resource
    private SysFileService sysFileService;
    @Resource
    private AjJsService ajJsService;
    @Resource
    private AjActivationCodeService ajActivationCodeService;
    @Resource
    private AjAdService ajAdService;
    @Resource
    private AjPhoneService ajPhoneService;

    /**
     * 根据js的lid，判断当前是否是最新的版本
     *
     * @return 返回最新的Js版本，并且返回下载url
     */
    @RequestMapping(params = "method=getLastestAjJs")
    public ResponseVo getLastestAjJs(HttpServletRequest request){
        //获取js
        String js_lid = request.getParameter("jsLid");
        if(StringUtils.isAnyBlank(js_lid)){
            return sendFail("参数错误");
        }
        AjJs ajJs = ajJsService.get(Integer.valueOf(js_lid));

        ///获取js所属的模块下最新的版本（因为在version不重复的前提下，可以使用order By + limit这种方法找出最值）
        QueryFilter maxFilter = new QueryFilter(_LayuiUtil.FIRST_PAGE, 1);
        maxFilter.where(AjJs.IS_DELETE, Where.eq, Common.IS_NO);
        maxFilter.where(AjJs.JM_LID, Where.eq, ajJs.getJmLid());
        maxFilter.orderBy(AjJs.VERSION, OrderBy.DESC);

        AjJs lastestJs = ajJsService.get(maxFilter);
        //sysFileService.getFiles(lastestJs, "jsFile"); //改用百度云分享
        return sendSuccess(lastestJs);
    }

    /**
     * 验证是否有权限
     * @return
     */
    @RequestMapping(params = "method=VerifyAuthority")
    public ResponseVo VerifyAuthority(HttpServletRequest request){
        String jsLid = request.getParameter("jsLid");
        String phoneBrand = request.getParameter("phoneBrand");
        String phoneImei = request.getParameter("phoneImei");
        String phoneWidth = request.getParameter("phoneWidth");
        String phoneHeight = request.getParameter("phoneHeight");
        String androidVersion = request.getParameter("androidVersion");
        if(StringUtils.isAnyBlank(jsLid, phoneBrand, phoneImei)){
            return sendFail("参数错误");
        }

        //保存手机设备信息
        QueryFilter phoneFilter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        phoneFilter.where(AjPhone.BRAND, Where.eq, phoneBrand);
        phoneFilter.where(AjPhone.IMEI, Where.eq, phoneImei);
        if(0 == ajPhoneService.getCount(phoneFilter)){
            AjPhone ajPhone = new AjPhone();
            ajPhone.setBrand(phoneBrand);
            ajPhone.setImei(phoneImei);
            ajPhone.setAndroidVersion(androidVersion);
            ajPhone.setWidth(phoneWidth);
            ajPhone.setHeight(phoneHeight);
            ajPhoneService.add(ajPhone);
        }

        //校验当前手机是否有权限运行jsLid脚本
        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where(AjActivationCode.JS_LID, Where.eq, jsLid);
        filter.where(AjActivationCode.PHONE_BRAND, Where.eq, phoneBrand);
        filter.where(AjActivationCode.PHONE_IMEI, Where.eq, phoneImei);
        AjActivationCode ajActivationCode = ajActivationCodeService.get(filter);
        if(null == ajActivationCode){
            return sendFail("验证失败");
        }else{
            return sendSuccess();
        }
    }

    /**
     * 验证激活码
     * @return
     */
    @RequestMapping(params = "method=VerifyActivationCode")
    public ResponseVo VerifyActivationCode(HttpServletRequest request){
        String activationCode = request.getParameter("activationCode");
        String jsLid = request.getParameter("jsLid");
        String phoneBrand = request.getParameter("phoneBrand");
        String phoneImei = request.getParameter("phoneImei");
        if(StringUtils.isAnyBlank(activationCode, jsLid, phoneBrand, phoneImei)){
            return sendFail("参数错误");
        }

        //查询是否有该激活码
        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where(AjActivationCode.CODE, Where.eq, activationCode);
        filter.where(AjActivationCode.JS_LID, Where.eq, jsLid);
        AjActivationCode ajActivationCode = ajActivationCodeService.get(filter);
        if(null == ajActivationCode){
            return sendFail("激活码不存在！");
        }

        //激活码还没被使用过，而且还未绑定手机品牌、手机唯一码
        if(Common.IS_NO == ajActivationCode.getIsUse()){
            ajActivationCode.setPhoneBrand(phoneBrand);
            ajActivationCode.setPhoneImei(phoneImei);
            ajActivationCode.setIsUse(Common.IS_YEW);
            ajActivationCode.setOperatorTime(_TimeUtil.now());
            ajActivationCodeService.update(ajActivationCode);
            return sendSuccess("激活成功");
        //激活码已经绑定过了手机品牌、手机唯一码
        }else{
            //如果当前的js版本、手机品牌、手机唯一码都跟激活码里面的信息相同，则验证通过
            if(jsLid.equals(ajActivationCode.getJsLid().toString())
                    && phoneBrand.equals(ajActivationCode.getPhoneBrand())
                    && phoneImei.equals(ajActivationCode.getPhoneImei())){
                return sendSuccess("激活成功");
            }else{
                return sendFail("激活码无效，该激活码已经被其他手机使用过了");
            }
        }
    }

    /**
     * 获取广告接口
     */
    @RequestMapping(params = "method=listAjAd")
    public ResponseVo listAjAd(HttpServletRequest request){
        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where(AjAd.IS_USE, Where.eq, Common.IS_YEW);
        filter.orderBy(AjAd.TYPE, OrderBy.ASC);
        List<AjAd> list = ajAdService.getAll(filter);
        return sendSuccess(list);
    }

}
